package com.ruoyi.production.service.impl.fertilizer;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.production.domain.fertilizer.FertilizerAmount;
import com.ruoyi.production.mapper.fertilizer.FertilizerAmountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.production.mapper.fertilizer.FertilizerPurchaseMapper;
import com.ruoyi.production.domain.fertilizer.FertilizerPurchase;
import com.ruoyi.production.service.fertilizer.IFertilizerPurchaseService;

/**
 * 化肥购买信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-12
 */
@Service
public class FertilizerPurchaseServiceImpl implements IFertilizerPurchaseService
{
    @Autowired
    private FertilizerPurchaseMapper fertilizerPurchaseMapper;
    @Autowired
    private FertilizerAmountMapper fertilizerAmountMapper;

    /**
     * 查询化肥购买信息
     *
     * @param fertilizerPurchaseId 化肥购买信息ID
     * @return 化肥购买信息
     */
    @Override
    public FertilizerPurchase selectFertilizerPurchaseById(Long fertilizerPurchaseId)
    {
        return fertilizerPurchaseMapper.selectFertilizerPurchaseById(fertilizerPurchaseId);
    }

    /**
     * 查询化肥购买信息列表
     *
     * @param fertilizerPurchase 化肥购买信息
     * @return 化肥购买信息
     */
    @Override
    public List<FertilizerPurchase> selectFertilizerPurchaseList(FertilizerPurchase fertilizerPurchase)
    {
        return fertilizerPurchaseMapper.selectFertilizerPurchaseList(fertilizerPurchase);
    }

    /**
     * 新增化肥购买信息
     *
     * @param fertilizerPurchase 化肥购买信息
     * @return 结果
     */
    @Override
    public int insertFertilizerPurchase(FertilizerPurchase fertilizerPurchase)
    {
        return fertilizerPurchaseMapper.insertFertilizerPurchase(fertilizerPurchase);
    }

    /**
     * 修改化肥购买信息
     *
     * @param fertilizerPurchase 化肥购买信息
     * @return 结果
     */
    @Override
    public int updateFertilizerPurchase(FertilizerPurchase fertilizerPurchase)
    {
        Long fertilizerPurchaseId = fertilizerPurchase.getFertilizerPurchaseId();
        //数据库存储的数据
        FertilizerPurchase PurchaseDB = selectFertilizerPurchaseById(fertilizerPurchaseId);
        //数据库存储的原数据的总量
        FertilizerAmount originAmounts = new FertilizerAmount();
        originAmounts.setBrand(PurchaseDB.getBrand());
        originAmounts.setFertilizerNameId(PurchaseDB.getFertilizerNameId());
        originAmounts.setFertilizerTypeId(PurchaseDB.getFertilizerTypeId());
        originAmounts.setOriginCompany(PurchaseDB.getOriginCompany());
        originAmounts.setNpkContent(PurchaseDB.getNpkContent());
        List<FertilizerAmount> amounts = fertilizerAmountMapper.selectFertilizerAmountList(originAmounts);
        FertilizerAmount originAmount = amounts.get(0);
        if (PurchaseDB.getBrand().equals(fertilizerPurchase.getBrand()) &&
                PurchaseDB.getOriginCompany().equals(fertilizerPurchase.getOriginCompany()) &&
                PurchaseDB.getFertilizerNameId().equals(fertilizerPurchase.getFertilizerNameId()) &&
                PurchaseDB.getFertilizerTypeId().equals(fertilizerPurchase.getFertilizerTypeId())&&
                PurchaseDB.getNpkContent().equals(fertilizerPurchase.getNpkContent())
        ) {
            originAmount.setStorageAmount(originAmount.getStorageAmount()+fertilizerPurchase.getPurchaseAmount()-PurchaseDB.getPurchaseAmount());
            fertilizerAmountMapper.updateFertilizerAmount(originAmount);
        }else {
            FertilizerAmount newAmounts = new FertilizerAmount();
            newAmounts.setFertilizerTypeId(fertilizerPurchase.getFertilizerTypeId());
            newAmounts.setFertilizerNameId(fertilizerPurchase.getFertilizerNameId());
            newAmounts.setBrand(fertilizerPurchase.getBrand());
            newAmounts.setOriginCompany(fertilizerPurchase.getOriginCompany());
            newAmounts.setNpkContent(fertilizerPurchase.getBrand());
            List<FertilizerAmount> amounts1 = fertilizerAmountMapper.selectFertilizerAmountList(newAmounts);
            if (StringUtils.isEmpty(amounts1)){
                newAmounts.setStorageAmount(fertilizerPurchase.getPurchaseAmount());
                fertilizerAmountMapper.insertFertilizerAmount(newAmounts);
            }else {
                FertilizerAmount newAmount = amounts1.get(0);
                newAmount.setStorageAmount(newAmount.getStorageAmount()+fertilizerPurchase.getPurchaseAmount());
                fertilizerAmountMapper.updateFertilizerAmount(newAmount);
            }
            originAmount.setStorageAmount(originAmount.getStorageAmount()-PurchaseDB.getPurchaseAmount());
            fertilizerAmountMapper.updateFertilizerAmount(originAmount);
        }

        return fertilizerPurchaseMapper.updateFertilizerPurchase(fertilizerPurchase);
    }

    /**
     * 批量删除化肥购买信息
     *
     * @param fertilizerPurchaseIds 需要删除的化肥购买信息ID
     * @return 结果
     */
    @Override
    public int deleteFertilizerPurchaseByIds(Long[] fertilizerPurchaseIds)
    {
        for (Long fertilizerPurchaseId : fertilizerPurchaseIds) {
            FertilizerPurchase fertilizerPurchase = selectFertilizerPurchaseById(fertilizerPurchaseId);
            FertilizerAmount fertilizerAmount = new FertilizerAmount();
            fertilizerAmount.setFertilizerNameId(fertilizerPurchase.getFertilizerNameId());
            fertilizerAmount.setFertilizerTypeId(fertilizerPurchase.getFertilizerTypeId());
            fertilizerAmount.setBrand(fertilizerPurchase.getBrand());
            fertilizerAmount.setOriginCompany(fertilizerPurchase.getOriginCompany());
            fertilizerAmount.setNpkContent(fertilizerPurchase.getNpkContent());
            List<FertilizerAmount> amounts = fertilizerAmountMapper.selectFertilizerAmountList(fertilizerAmount);
            FertilizerAmount fertilizerAmount1 = amounts.get(0);
            fertilizerAmount1.setStorageAmount(fertilizerAmount1.getStorageAmount() - fertilizerPurchase.getPurchaseAmount());
            fertilizerAmountMapper.updateFertilizerAmount(fertilizerAmount1);
        }
        return fertilizerPurchaseMapper.deleteFertilizerPurchaseByIds(fertilizerPurchaseIds);
    }

    /**
     * 删除化肥购买信息信息
     *
     * @param fertilizerPurchaseId 化肥购买信息ID
     * @return 结果
     */
    @Override
    public int deleteFertilizerPurchaseById(Long fertilizerPurchaseId)
    {
        FertilizerPurchase fertilizerPurchase = selectFertilizerPurchaseById(fertilizerPurchaseId);
        FertilizerAmount fertilizerAmount = new FertilizerAmount();
        fertilizerAmount.setFertilizerNameId(fertilizerPurchase.getFertilizerNameId());
        fertilizerAmount.setFertilizerTypeId(fertilizerPurchase.getFertilizerTypeId());
        fertilizerAmount.setBrand(fertilizerPurchase.getBrand());
        fertilizerAmount.setOriginCompany(fertilizerPurchase.getOriginCompany());
        fertilizerAmount.setNpkContent(fertilizerPurchase.getNpkContent());
        List<FertilizerAmount> amounts = fertilizerAmountMapper.selectFertilizerAmountList(fertilizerAmount);
        FertilizerAmount fertilizerAmount1 = amounts.get(0);
        fertilizerAmount1.setStorageAmount(fertilizerAmount1.getStorageAmount() - fertilizerPurchase.getPurchaseAmount());
        fertilizerAmountMapper.updateFertilizerAmount(fertilizerAmount1);
        return fertilizerPurchaseMapper.deleteFertilizerPurchaseById(fertilizerPurchaseId);
    }

    /**
     * 插入化肥购买表更新化肥量表
     * @param fertilizerPurchase
     * @return
     */
    @Override
    public int insertPurchaseUpdateAmount(FertilizerPurchase fertilizerPurchase){
        FertilizerAmount fertilizerAmount = new FertilizerAmount();
        fertilizerAmount.setFertilizerTypeId(fertilizerPurchase.getFertilizerTypeId());
        fertilizerAmount.setFertilizerNameId(fertilizerPurchase.getFertilizerNameId());
        fertilizerAmount.setBrand(fertilizerPurchase.getBrand());
        fertilizerAmount.setNpkContent(fertilizerPurchase.getNpkContent());
        fertilizerAmount.setOriginCompany(fertilizerPurchase.getOriginCompany());
        List<FertilizerAmount> fertilizerAmounts = fertilizerAmountMapper.selectFertilizerAmountList(fertilizerAmount);
        fertilizerAmount.setStorageAmount(fertilizerPurchase.getPurchaseAmount());
        if (!StringUtils.isEmpty(fertilizerAmounts)) {
            FertilizerAmount amount = fertilizerAmounts.get(0);
            amount.setStorageAmount(amount.getStorageAmount() + fertilizerPurchase.getPurchaseAmount());
            fertilizerAmountMapper.updateFertilizerAmount(amount);
        } else {
            fertilizerAmountMapper.insertFertilizerAmount(fertilizerAmount);
        }
        return insertFertilizerPurchase(fertilizerPurchase);
    }
}
